/**
* 文 件 名 :FineGrainedAuthorityServiceImpl.java
    * CopyRright (c) 2012-0418:
* 文件编号：
* 创 建 人：薛琦
* 日    期：2012-04-18
* 修 改 人：
* 日   期：
* 描   述：FineGrainedAuthorityService的实现类
* 版 本 号：
*/ 
package com.tjtt.tdm.system.service.impl;

import java.util.Collection;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.tjtt.tdm.exception.DaoException;
import com.tjtt.tdm.exception.ServiceException;
import com.tjtt.tdm.system.bean.Rolemenu;
import com.tjtt.tdm.system.dao.FineGrainedAuthorityDao;
import com.tjtt.tdm.system.dao.RoleMenuDao;
import com.tjtt.tdm.system.service.FineGrainedAuthorityService;
/**
* CopyRright (c)2012-0418:   <天健通泰科技 >                                                                
* Module ID:  
    * Comments:  实现类，对于角色对应的菜单按钮在数据库的增删改操作，比DAO控制粒度较粗。                                        
* JDK version used:      <JDK1.6>                             
* Namespace:        com.tjtt.tdm.system.service.impl                           
* Author：        xueqi               
* Create Date：  2012-04-18
* Modified By：                                     
* Modified Date:                                  
* Why & What is modified  <修改原因描述>   
* Version:                  <版本号>                      
*/ 
public class FineGrainedAuthorityServiceImpl implements
		FineGrainedAuthorityService {
	private static final Log log = LogFactory.getLog(FineGrainedAuthorityServiceImpl.class);
	private FineGrainedAuthorityDao fineGrainedAuthorityDao;
	private RoleMenuDao roleMenuDao;
     private List resultList=null;
	public void setFineGrainedAuthorityDao(
			FineGrainedAuthorityDao fineGrainedAuthorityDao) {
		this.fineGrainedAuthorityDao = fineGrainedAuthorityDao;
	}

	public void delButtonsByParam(Long menuId, Long buttonId) {
		
		this.fineGrainedAuthorityDao.delButtonsByParam(menuId, buttonId);
	}

	public void delRoleButtonsByRoleId(Long roleId) {
		
		this.fineGrainedAuthorityDao.delRoleButtonsByRoleId(roleId);
	}

	

	public RoleMenuDao getRoleMenuDao() {
		return roleMenuDao;
	}

	public void setRoleMenuDao(RoleMenuDao roleMenuDao) {
		this.roleMenuDao = roleMenuDao;
	}

	public List getButtonsByMenuId(Long menuId) throws ServiceException  {
		
		
		try {
			resultList=this.fineGrainedAuthorityDao.getButtonsByMenuId(menuId);
		} catch (DaoException e) {
			
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
		
		return resultList;
	}

	public List getButtonsByRoleId(Long roleId) throws ServiceException  {
		
		try {
			resultList=this.fineGrainedAuthorityDao.getButtonsByRoleId(roleId);
		} catch (DaoException e) {
			
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
		
		return resultList;
	}

	public void saveButtons(List buttonList) throws ServiceException  {
		
		try {
			this.fineGrainedAuthorityDao.saveButtons(buttonList);
		} catch (DaoException e) {
			
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
	}

	public void saveRoleButtons(List roleButtonsList) throws ServiceException  {
		
		try {
			this.fineGrainedAuthorityDao.saveRoleButtons(roleButtonsList);
		} catch (DaoException e) {
			
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
	}

	public void delDatas(Collection collection) {
		
		this.fineGrainedAuthorityDao.delDatas(collection);
	}

	public List getRoleButtons(Long roleid) {
		
		return this.fineGrainedAuthorityDao.getRoleButtons(roleid);
	}
   
	public void updateRoleMenuByRoleId(Long roleid, List<Long> menuIds, List buttonList) throws ServiceException {
		
		try {
			roleMenuDao.removeByParams(Rolemenu.class,"roleid", new Long[]{roleid});  //根据roleId删除角色表中记录
			
			for (Long menuId : menuIds) {
				Rolemenu roleMenu = new Rolemenu();
				roleMenu.setMenuid(menuId);
				roleMenu.setRoleid(roleid);
				roleMenuDao.save(roleMenu);
			}
			this.fineGrainedAuthorityDao.delRoleButtonsByRoleId(roleid);
			this.fineGrainedAuthorityDao.saveButtons(buttonList);
		} catch (DaoException e) {
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
	}

	public List getAllMenus() {
		return this.fineGrainedAuthorityDao.getAllMenus();
	}

	public Long findExistData(Object[] args) {
		return this.fineGrainedAuthorityDao.findExistData(args);
	}

	public List getButtonsByRoleId(Long[] roleIds) throws DaoException {
		return this.fineGrainedAuthorityDao.getButtonsByRoleId(roleIds);
	}

	public List getRoleButtons(Long[] roleids) {
		return this.getRoleButtons(roleids);
	}

	
   
    
    
}
